# -*- coding:utf-8 -*-
from flask import Flask
from celery import Celery
from flask_login import LoginManager
from flask_sqlalchemy import SQLAlchemy
from flask_wtf.csrf import CSRFProtect
from flask_cors import CORS
from datetime import timedelta
from app_config import config, Config


app_ = Flask(__name__)
CORS(app_, supports_credentials=True)  # 用于处理跨域问题
db = SQLAlchemy()
celery = Celery(__name__, broker=Config.CELERY_BROKER_URL)
csrf = CSRFProtect()
app_.permanent_session_lifetime = timedelta(minutes=120)

# 登录模块
login_manager = LoginManager()
# login_manager.session_protection = 'strong'
# login_manager.login_view = 'web.login'
# login_manager.login_message = u""
# login_manager.login_message_category = "info"


def create_app(config_name):
	app_.config.from_object(config[config_name])
	app_.config['SECRET_KEY'] = "1234456789999999995"
	app_.permanent_session_lifetime = timedelta(minutes=120)
	config[config_name].init_app(app_)

	login_manager.init_app(app_)
	db.init_app(app_)
	csrf.init_app(app_)

	from .api import nds_api as api_blueprint
	app_.register_blueprint(blueprint=api_blueprint, url_prefix='/v2')
	return app_


